//using UnityEngine;
//using UnityEngine.Purchasing;
//using UnityEngine.Purchasing.Security;

//namespace PK.InAppPurchase
//{
//	public abstract class BaseStoreManager : MonoBehaviour, IStoreListener
//	{
//		public enum PurchaseFailureMessage
//		{
//			PurchasingUnavailable,
//			ExistingPurchasePending,
//			ProductUnavailable,
//			SignatureInvalid,
//			UserCancelled,
//			PaymentDeclined,
//			DuplicateTransaction,
//			NotInitialized,
//			UnavailableOnApp,
//			Unknown
//		}

//		public enum PurchaseResult
//		{
//			Success,
//			Cancelled,
//			Refunded,
//			Deferred
//		}

//		[SerializeField]
//		private StoreProducts _storeProducts;

//		protected IStoreController _storeController;

//		private IExtensionProvider _extensions;

//		private void Start()
//		{
//			InitializeStore(_storeProducts);
//		}

//		private void InitializeStore(StoreProducts storeProducts)
//		{
//			InitializeUnityPurchasing(storeProducts);
//		}

//		private void InitializeUnityPurchasing(StoreProducts storeProducts)
//		{
//			StandardPurchasingModule first = StandardPurchasingModule.Instance();
//			ConfigurationBuilder configurationBuilder = ConfigurationBuilder.Instance(first);
//			StoreProduct[] storeProductArray = storeProducts.StoreProductArray;
//			foreach (StoreProduct storeProduct in storeProductArray)
//			{
//				configurationBuilder.AddProduct(storeProduct.StoreId, storeProduct.Type);
//			}
//			UnityPurchasing.Initialize(this, configurationBuilder);
//		}

//		public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
//		{
//			_storeController = controller;
//			_extensions = extensions;
//			RestoreTransactionsIos();
//			_extensions.GetExtension<IAppleExtensions>().RegisterPurchaseDeferredListener(OnDeferred);
//		}

//		public void OnDeferred(Product product)
//		{
//			if (_storeProducts.StoreProductDictionary.ContainsKey(product.definition.id))
//			{
//				ProcessPurchaseResult(PurchaseResult.Deferred, _storeProducts.StoreProductDictionary[product.definition.id], string.Empty);
//			}
//		}

//		public abstract void ProcessPurchaseResult(PurchaseResult result, StoreProduct storeProduct, string transactionId);

//		public void RestoreTransactionsIos()
//		{
//			_extensions.GetExtension<IAppleExtensions>().RestoreTransactions(OnTransactionsRestored);
//		}

//		protected abstract void OnTransactionsRestored(bool success);

//		public abstract void OnInitializeFailed(InitializationFailureReason error);

//		protected void PurchaseStoreProduct(StoreProduct storeProduct)
//		{
//			if (IsInitialized())
//			{
//				Product product = _storeController.products.WithID(storeProduct.StoreId);
//				if (product != null && product.availableToPurchase)
//				{
//					_storeController.InitiatePurchase(product);
//				}
//				else
//				{
//					OnPurchaseFailed(storeProduct, PurchaseFailureMessage.UnavailableOnApp);
//				}
//			}
//			else
//			{
//				OnPurchaseFailed(storeProduct, PurchaseFailureMessage.NotInitialized);
//			}
//		}

//		public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
//		{
//			CrossPlatformValidator crossPlatformValidator = new CrossPlatformValidator(GooglePlayTangle.Data(), AppleTangle.Data(), Application.identifier);
//			IPurchaseReceipt[] array = null;
//			try
//			{
//				array = crossPlatformValidator.Validate(e.purchasedProduct.receipt);
//			}
//			catch (IAPSecurityException)
//			{
//				return PurchaseProcessingResult.Complete;
//			}
//			if (array == null)
//			{
//				return PurchaseProcessingResult.Complete;
//			}
//			IPurchaseReceipt[] array2 = array;
//			foreach (IPurchaseReceipt productReceipt in array2)
//			{
//				if (IsValidReceipt(productReceipt))
//				{
//					ProcessReceipt(productReceipt);
//				}
//			}
//			return PurchaseProcessingResult.Complete;
//		}

//		private void ProcessReceipt(IPurchaseReceipt productReceipt)
//		{
//			if (productReceipt is GooglePlayReceipt)
//			{
//				GooglePlayReceipt googlePlayReceipt = productReceipt as GooglePlayReceipt;
//				ProcessPurchaseResult(MapGooglePurchaseResult(googlePlayReceipt), _storeProducts.StoreProductDictionary[productReceipt.productID], googlePlayReceipt.transactionID);
//			}
//			else if (productReceipt is AppleInAppPurchaseReceipt)
//			{
//				AppleInAppPurchaseReceipt appleInAppPurchaseReceipt = productReceipt as AppleInAppPurchaseReceipt;
//				ProcessPurchaseResult(MapApplePurchaseResult(appleInAppPurchaseReceipt), _storeProducts.StoreProductDictionary[productReceipt.productID], appleInAppPurchaseReceipt.transactionID);
//			}
//		}

//		private bool IsValidReceipt(IPurchaseReceipt productReceipt)
//		{
//			return _storeProducts.StoreProductDictionary.ContainsKey(productReceipt.productID);
//		}

//		private static PurchaseResult MapGooglePurchaseResult(GooglePlayReceipt receipt)
//		{
//			switch (receipt.purchaseState)
//			{
//			case GooglePurchaseState.Purchased:
//				return PurchaseResult.Success;
//			case GooglePurchaseState.Cancelled:
//				return PurchaseResult.Cancelled;
//			case GooglePurchaseState.Refunded:
//				return PurchaseResult.Refunded;
//			default:
//				return PurchaseResult.Cancelled;
//			}
//		}

//		private static PurchaseResult MapApplePurchaseResult(AppleInAppPurchaseReceipt receipt)
//		{
//			return PurchaseResult.Success;
//		}

//		public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
//		{
//			OnPurchaseFailed(product, ConvertFailureReason(failureReason));
//		}

//		private static PurchaseFailureMessage ConvertFailureReason(PurchaseFailureReason failureReason)
//		{
//			switch (failureReason)
//			{
//			case PurchaseFailureReason.PurchasingUnavailable:
//				return PurchaseFailureMessage.PurchasingUnavailable;
//			case PurchaseFailureReason.ExistingPurchasePending:
//				return PurchaseFailureMessage.ExistingPurchasePending;
//			case PurchaseFailureReason.ProductUnavailable:
//				return PurchaseFailureMessage.ProductUnavailable;
//			case PurchaseFailureReason.SignatureInvalid:
//				return PurchaseFailureMessage.SignatureInvalid;
//			case PurchaseFailureReason.UserCancelled:
//				return PurchaseFailureMessage.UserCancelled;
//			case PurchaseFailureReason.PaymentDeclined:
//				return PurchaseFailureMessage.PaymentDeclined;
//			case PurchaseFailureReason.DuplicateTransaction:
//				return PurchaseFailureMessage.DuplicateTransaction;
//			case PurchaseFailureReason.Unknown:
//				return PurchaseFailureMessage.Unknown;
//			default:
//				return PurchaseFailureMessage.Unknown;
//			}
//		}

//		protected abstract void OnPurchaseFailed(Product product, PurchaseFailureMessage failureReason);

//		protected abstract void OnPurchaseFailed(StoreProduct storeProduct, PurchaseFailureMessage failureReason);

//		private bool IsInitialized()
//		{
//			return _storeController != null && _extensions != null;
//		}
//	}
//}
